/*
 * StudentSession
 * Enroll and drop classes Buisness APIs
 */
package edu.depaul.se.courseregistration.sessionbean;

import edu.depaul.se.courseregistration.jpa.courses.Section;
import edu.depaul.se.courseregistration.mdb.p2p.ProcessRequest;
import edu.depaul.se.courseregistration.jpa.schedule.EnrolledClass;
import edu.depaul.se.courseregistration.jpa.schedule.Schedule;
import edu.depaul.se.courseregistration.jpa.user.Person;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
/**
 *
 * @author Group2
 */
@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class StudentEJB{
    
    @PersistenceContext(unitName="CourseRegistrationModulePU")
    private EntityManager em;  

    //@Override
    public int EnrollCourse(int sectionId, int personId){
    int result =1; //fail   
    Person person = em.createNamedQuery(Person.PERSON_QUERY_BY_ID, Person.class).
    setParameter("personID", personId).getSingleResult();
    Section section = em.createNamedQuery(Section.SECTION_QUERY_BY_ID, Section.class).
    setParameter("sectionId", sectionId).getSingleResult();
    EnrolledClass enroll = new EnrolledClass();
    if(section.getSectionStaus())
       {
         enroll.setSection(section);
         enroll.setPersonID(person);
         enroll.setGrade(null);
         em.persist(enroll);
         result = 0; //sucess
        }          
        
    //Call the ProcessEnroll session to send the message
    ProcessRequest processEnroll = new ProcessRequest();
    processEnroll.processEnrollRequest(enroll, result,ProcessRequest.ENROLL);

    return result;
    }
    //@Override
      public int dropCourse(int sectionId, int personId){
          int result = 1; //Fail case
       Person person = em.find(Person.class, personId);
       Section section = em.find(Section.class, sectionId); 
       TypedQuery tq = em.createNamedQuery(Schedule.CLASS_QUERY_BY_SECTION_AND_PERSON, Schedule.class);
       tq.setParameter("sectionId", section);
       tq.setParameter("personID", person);
       Schedule enroll = (EnrolledClass) tq.getSingleResult();
       if(enroll!=null)
       {
         em.remove(enroll);
         result=0; //sucess
       }
       //Call the ProcessEnroll session to send the message
       ProcessRequest processEnroll = new ProcessRequest();
       processEnroll.processEnrollRequest((EnrolledClass) enroll, result,ProcessRequest.DROP);
       return  result;
    }
    //@Override    
       public String getClassGradebyStudentId(int  sectionId, int personId){
       TypedQuery tq = em.createNamedQuery(EnrolledClass.CLASS_QUERY_BY_SECTION_AND_PERSON, EnrolledClass.class);
       Person person = em.find(Person.class, personId);
       Section section = em.find(Section.class, sectionId);
       tq.setParameter("sectionId", section);
       tq.setParameter("personID", person);
       EnrolledClass enroll = (EnrolledClass) tq.getSingleResult();
       String grade = enroll.getGrade();
       return grade;
    }
     
     //@Override
     public List < EnrolledClass> getSchedule(int personId){
      Person person = em.find(Person.class, personId);
      return em.createNamedQuery(Schedule.CLASS_QUERY_BY_PERSON_ID, EnrolledClass.class).
      setParameter("personID", person).getResultList();
    }
    
}
